//
//  AccountViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/1.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "AccountViewController.h"
#import "AddAccountViewController.h"
#import "EditAccountViewController.h"
#import "UserModel.h"
@interface AccountViewController ()
@property(nonatomic,assign)NSInteger acType;
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor colorF];
    self.navigationItem.title = @"账户管理";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(addAction)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorQ];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    
    
    
    [self initToolBar];
    
    
}



- (void)initToolBar
{

    //如果传ClassName就是自定义  如果不传就是系统的  刷新列表dataArr需要主动
    
    self.tableView =[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT+50, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-BOTTOM_HEIGHT-50)  withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        
        EditAccountViewController *vc=[[EditAccountViewController alloc]init];
        if ([self.tableView.className isEqualToString:@"TableViewCell"]) {
            TableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"管理员";
        }else if (self.acType==0)
        {
            StoreCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"门店";
        }
        else if ([self.tableView.className isEqualToString:@"RepairerCell"])
        {
            RepairerCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"修理商";
        }
        else if (self.acType==1)
        {
            PoliceCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"电动车厂商";
        }
        else if (self.acType==2)
        {
            PoliceCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"民警";
        }
        else if (self.acType==3)
        {
            PropertyCell *cell=[tableView cellForRowAtIndexPath:indexPath];
            vc.model=cell.dataDic.model;
            vc.type=@"物业";
        }
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    
    [self.view addSubview:self.tableView];
    
    

    
    
    
    UISegmentedControl *TypeSegmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                   [NSArray arrayWithObjects:
                                                    
                                                    @"门店",
                                                    
                                                    @"电动车厂商",
                                                    @"民警",
                                                    @"物业",
                                                    nil]];
    TypeSegmentedControl.apportionsSegmentWidthsByContent = YES;
    TypeSegmentedControl.layer.masksToBounds=YES;
    TypeSegmentedControl.layer.cornerRadius=5;
    // 设置整体的色调
    TypeSegmentedControl.tintColor = [UIColor colorF];
    NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor color6],NSForegroundColorAttributeName,[UIFont systemFontOfSize:12.0],NSFontAttributeName,nil];
    [TypeSegmentedControl setTitleTextAttributes:dics forState:UIControlStateNormal];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor colorQ]]
                                 forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor whiteColor]]
                                 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [TypeSegmentedControl addTarget:self action:@selector(TypeAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:TypeSegmentedControl];
    [TypeSegmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(TOP_HEIGHT+10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-20, 30));
        
    }];

    
    TypeSegmentedControl.selectedSegmentIndex=0;
    [self TypeAction:TypeSegmentedControl];

}
-(void)addAction
{
    AddAccountViewController *vc=[[AddAccountViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)TypeAction:(UISegmentedControl *)segmentedControl
{
    
   
    self.acType = segmentedControl.selectedSegmentIndex;
    switch (self.acType) {
//        case 0:
//        {
//
//            NSString *URLString = @"/axq/admin/list";
//            NSDictionary *parameters = @{@"role": @"admin"};
//            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
//
//
//
//                NSLog(@"%@",responseObject);
//                self.tableView.className=@"TableViewCell";
//                NSMutableArray *itemArr=[NSMutableArray array];
//
//
//
//
//
//
//                NSString *code = responseObject[@"code"];
//                if([code isEqualToString:@"10008"]){
//                    NSArray *data=responseObject[@"data"];
//                    if (data.count) {
//                        for (int i=0; i<data.count; i++) {
//
//                            NSDictionary *mData=data[i];
//
//
//                            UserModel *model=[UserModel new];
//
//                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
//                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
//                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
//                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
//                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
//                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
//                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
//                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
//                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
//                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
//                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
//                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
//                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
//                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
//                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
//                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
//
//
//
//                            cellData *dataObj=[[cellData alloc]init];
//                            dataObj.image=model.headimg;
//                            dataObj.text=model.userTitle;
//                            dataObj.detailText=model.loginId;
//                            dataObj.numText=model.userCode;
//                            dataObj.model=model;
//                            [itemArr addObject:dataObj];
//
//
//
//                        }
//                    }
//                }
//                else
//                {
//                    NSString *str=responseObject[@"msg"];
//
//                    [self.view makeToast:str];
//                }
//                [self.tableView.dataArray addObject:itemArr];
//                [self.tableView reloadData];
//            } RequestFailed:^(NSError *error) {
//                self.tableView.className=@"TableViewCell";
//                NSMutableArray *itemArr=[NSMutableArray array];
//
//                [self.tableView.dataArray addObject:itemArr];
//                [self.tableView reloadData];
//            }];
//
//
//
//
//
//        }
//            break;
        case 0:
        {
            
            NSString *URLString = @"/axq/admin/list";
            NSDictionary *parameters = @{@"role": @"shop"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                LRLog(@"%@",responseObject);
                //清除数据并更换cell样式
                self.tableView.className=@"StoreCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code = responseObject[@"code"];
                if([code isEqualToString:@"10008"]){
                    NSArray *data=responseObject[@"data"];
                    if (data.count) {
                        for (int i=0; i<data.count; i++) {
                            
                            NSDictionary *mData=data[i];
                            
                            
                            UserModel *model=[UserModel new];
                            
                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
                            
                            
                            
                            
                            
                            StoreData *dataObj=[[StoreData alloc]init];
                            dataObj.image=model.headimg;
                            dataObj.text=model.userTitle;
                            dataObj.address=model.address;
                            dataObj.model=model;
                            [itemArr addObject:dataObj];
                            
                            
                            
                            
                            
                            
                            
                            
                            
                        }
                    }
                }
                else
                {
                    NSString *str=responseObject[@"msg"];
                    
                    [self.view makeToast:str];
                }
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                //清除数据并更换cell样式
                self.tableView.className=@"StoreCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
            
            
            
            
            
            
                
                
                
            
           

        }
            break;
//        case 1:
//        {
//
//
//            NSString *URLString = @"/axq/admin/list";
//            NSDictionary *parameters = @{@"role": @"repair"};
//            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
//                LRLog(@"%@",responseObject);
//                self.tableView.className=@"RepairerCell";
//                NSMutableArray *itemArr=[NSMutableArray array];
//                NSString *code = responseObject[@"code"];
//                if([code isEqualToString:@"10008"]){
//                    NSArray *data=responseObject[@"data"];
//                    if (data.count) {
//                        for (int i=0; i<data.count; i++) {
//
//                            NSDictionary *mData=data[i];
//
//
//                            UserModel *model=[UserModel new];
//
//                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
//                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
//                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
//                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
//                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
//                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
//                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
//                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
//                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
//                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
//                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
//                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
//                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
//                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
//                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
//                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
//
//
//                            repairerData *dataObj=[[repairerData alloc]init];
//                            dataObj.image=model.headimg;
//                            dataObj.text=model.userTitle;
//                            dataObj.address=model.address;
//                            [itemArr addObject:dataObj];
//
//
//
//
//
//
//                        }
//                    }
//                }
//                else
//                {
//                    NSString *str=responseObject[@"msg"];
//
//                    [self.view makeToast:str];
//                }
//                [self.tableView.dataArray addObject:itemArr];
//                [self.tableView reloadData];
//            } RequestFailed:^(NSError *error) {
//                self.tableView.className=@"RepairerCell";
//                NSMutableArray *itemArr=[NSMutableArray array];
//                [self.tableView.dataArray addObject:itemArr];
//                [self.tableView reloadData];
//            }];
//
//
//
//        }
//            break;
        case 1:
        {
            
            
            NSString *URLString = @"/axq/admin/list";
            NSDictionary *parameters = @{@"role": @"producer"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                //LRLog(@"%@",responseObject);
                self.tableView.className=@"PoliceCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code = responseObject[@"code"];
                if([code isEqualToString:@"10008"]){
                    NSArray *data=responseObject[@"data"];
                    if (data.count) {
                        for (int i=0; i<data.count; i++) {
                            
                            NSDictionary *mData=data[i];
                            
                            
                            UserModel *model=[UserModel new];
                            
                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
                            
                            
                            
                            
                            
                            policeData *dataObj=[[policeData alloc]init];
                            dataObj.image=model.headimg;
                            dataObj.name=model.userTitle;
                            dataObj.phone=model.telephone;
                            dataObj.model=model;
                            [itemArr addObject:dataObj];
                            
                            
                            
                        }
                    }
                }
                else
                {
                    NSString *str=responseObject[@"msg"];
                    
                    [self.view makeToast:str];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView.dataArray addObject:itemArr];
                    [self.tableView reloadData];
                });
                
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"PoliceCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
            
            
            
            
        }
            break;
        case 2:
        {
            
            
            NSString *URLString = @"/axq/admin/list";
            NSDictionary *parameters = @{@"role": @"police"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                //LRLog(@"%@",responseObject);
                self.tableView.className=@"PoliceCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code = responseObject[@"code"];
                if([code isEqualToString:@"10008"]){
                    NSArray *data=responseObject[@"data"];
                    if (data.count) {
                        for (int i=0; i<data.count; i++) {
                            
                            NSDictionary *mData=data[i];
                            
                            
                            UserModel *model=[UserModel new];
                            
                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
                            
                            
                            
                            
                            
                            policeData *dataObj=[[policeData alloc]init];
                            dataObj.image=model.headimg;
                            dataObj.name=model.userTitle;
                            dataObj.phone=model.telephone;
                            dataObj.model=model;
                            [itemArr addObject:dataObj];
                            
                            
                            
                        }
                    }
                }
                else
                {
                    NSString *str=responseObject[@"msg"];
                    
                    [self.view makeToast:str];
                }
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"PoliceCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
            
            
            
        }
            break;
        case 3:
        {
            
            
            
            NSString *URLString = @"/axq/admin/list";
            NSDictionary *parameters = @{@"role": @"property"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                //LRLog(@"%@",responseObject);
                self.tableView.className=@"PropertyCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code = responseObject[@"code"];
                if([code isEqualToString:@"10008"]){
                    NSArray *data=responseObject[@"data"];
                    if (data.count) {
                        for (int i=0; i<data.count; i++) {
                            
                            NSDictionary *mData=data[i];
                            
                            
                            UserModel *model=[UserModel new];
                            
                            model.addtime=[NSString stringWithFormat:@"%@",mData[@"addtime"]];
                            model.updatetime=[NSString stringWithFormat:@"%@",mData[@"updatetime"]];
                            model.lat=[NSString stringWithFormat:@"%@",mData[@"lat"]];
                            model.lon=[NSString stringWithFormat:@"%@",mData[@"lon"]];
                            model.password=[NSString stringWithFormat:@"%@",mData[@"password"]];
                            model.telephone=[NSString stringWithFormat:@"%@",mData[@"telephone"]];
                            model.userCode=[NSString stringWithFormat:@"%@",mData[@"userCode"]];
                            model.userRole=[NSString stringWithFormat:@"%@",mData[@"userRole"]];
                            model.userTitle=[NSString stringWithFormat:@"%@",mData[@"userTitle"]];
                            model.address=[NSString stringWithFormat:@"%@",mData[@"address"]];
                            model.contacts=[NSString stringWithFormat:@"%@",mData[@"contacts"]];
                            model.headimg=[NSString stringWithFormat:@"%@",mData[@"headimg"]];
                            model.someId=[NSString stringWithFormat:@"%@",mData[@"id"]];
                            model.linkman=[NSString stringWithFormat:@"%@",mData[@"linkman"]];
                            model.loginId=[NSString stringWithFormat:@"%@",mData[@"loginId"]];
                            model.status=[NSString stringWithFormat:@"%@",mData[@"status"]];
                            
                            
                            
                            
                            propertyData *dataObj=[[propertyData alloc]init];
                            dataObj.image=model.headimg;
                            dataObj.name=model.userTitle;
                            dataObj.phone=model.telephone;
                            dataObj.model=model;
                            [itemArr addObject:dataObj];
                            
                            
                            
                            
                        }
                    }
                }
                else
                {
                    NSString *str=responseObject[@"msg"];
                    
                    [self.view makeToast:str];
                }
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"PropertyCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
            
            
            
        }
            break;
            
        default:
            break;
    }
}
@end
